package com.masterclient.app.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.RectF;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.eoner.baselibrary.utils.DensityUtil;

public class GlideRoundTransform extends CenterCrop {

    private float radius = 10f;
    private int color;
    private String type;
    public static final String RADIUSTOP = "top";
    public static final String RADIUSBOTTOM = "bottom";

    public GlideRoundTransform(Context context, int dp, int color) {
        super();
        this.radius = DensityUtil.dip2px(dp);
        this.color = color;
    }

    public GlideRoundTransform(Context context, int dp) {
        super();
        this.radius = DensityUtil.dip2px(dp);
    }

    public GlideRoundTransform(Context context, int dp, String type) {
        super();
        this.type = type;
        this.radius = DensityUtil.dip2px(dp);
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        //glide4.0+
        Bitmap transform = super.transform(pool, toTransform, outWidth, outHeight);
        return roundCrop(pool, transform);
        //glide3.0
        //return roundCrop(pool, toTransform);
    }

    private Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;

        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        if (color != 0) {
            paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
        }
        RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rectF, radius, radius, paint);
        if (RADIUSTOP.equals(type)) { //下面不为圆角
            canvas.drawRect(0, canvas.getHeight() - radius, radius, canvas.getHeight(), paint);
            canvas.drawRect(canvas.getWidth() - radius, canvas.getHeight() - radius, canvas.getWidth(), canvas.getHeight(), paint);
        } else if (RADIUSBOTTOM.equals(type)) {//上角不为圆角
            canvas.drawRect(canvas.getWidth() - radius, 0, canvas.getWidth(), radius, paint);
            canvas.drawRect(0, 0, radius, radius, paint);
        }
        //        if (exceptLeftTop) { //左上角不为圆角
//            canvas.drawRect(0, 0, radius, radius, paint);
//        }
//        if (exceptRightTop) {//右上角不为圆角
//            canvas.drawRect(canvas.getWidth() - radius, 0, canvas.getWidth(), radius, paint);
//        }
//
//        if (exceptLeftBottom) {//左下角不为圆角
//            canvas.drawRect(0, canvas.getHeight() - radius, radius, canvas.getHeight(), paint);
//        }
//
//        if (exceptRightBotoom) {//右下角不为圆角
//            canvas.drawRect(canvas.getWidth() - radius, canvas.getHeight() - radius, canvas.getWidth(), canvas.getHeight(), paint);
//        }
        return result;
    }

}
